package cryptography;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Mouchicc & Mavs
 * @version 1.0
 */
public class Arcanum {

    String IN;
    String OUT;
    int KEY;

    public Arcanum(String path_in, String path_out, String key) {
        this.IN = path_in;
        this.OUT = path_out;
        this.KEY = Methods.createPass(key);
    }

    public Arcanum(String path_in, String key) {
        this.IN = path_in;
        this.KEY = Methods.createPass(key);
    }

    public String getExtension() {
        return IN.substring(IN.length() - 4, IN.length());
    }

    public void encrypt() {
        try {
            this.OUT = IN.substring(0, IN.length() - 4) + "_arcanum_enc.png";
            if (getExtension().equals(".png")) {
                File input = new File(IN);
                BufferedImage image = ImageIO.read(input);

                File output = new File(IN.substring(0, IN.length() - 4) + ".png");
                IN = IN.substring(0, IN.length() - 4) + ".png";
                ImageIO.write(image, "png", output);
                input.delete();
            }

            BufferedImage in = ImageIO.read(new File(IN));
            BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < in.getWidth(); x++) {
                for (int y = 0; y < in.getHeight(); y++) {
                    int rgb = in.getRGB(x, y);
                    Color c = new Color(rgb);

                    int red = c.getRed();
                    int green = c.getGreen();
                    int blue = c.getBlue();


                    red = Methods.mod((red * KEY), 255);
                    green = Methods.mod((green * KEY), 255);
                    blue = Methods.mod((blue * KEY), 255);

                    Color clr = new Color(red, green, blue);
                    out.setRGB(x, y, clr.getRGB());

                }
            }

            File outputfile = new File(OUT);
            ImageIO.write(out, "png", outputfile);

        } catch (IOException e) {
        }
    }

    public void decrypt() {
        try {
            this.OUT = IN.split("_")[0] + "_arcanum_dec.png";
            IN = IN.split("_")[0] + ".png";
            BufferedImage in = ImageIO.read(new File(IN)); BufferedImage out_ = in;
            BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < in.getWidth(); x++) {
                for (int y = 0; y < in.getHeight(); y++) {
                    int rgb = in.getRGB(x, y);
                    Color c = new Color(rgb);

                    int red = c.getRed();
                    int green = c.getGreen();
                    int blue = c.getBlue();

                    red = Methods.inverseMod(KEY, 255, red);
                    green = Methods.inverseMod(KEY, 255, green);
                    blue = Methods.inverseMod(KEY, 255, blue);

                    Color clr = new Color(red, green, blue);
                    out.setRGB(x, y, clr.getRGB());
                }
            }

            File outputfile = new File(OUT);
            ImageIO.write(out_, "png", outputfile);

        } catch (IOException e) {
        }
    }
}
